package com.evaluationSystem.service.impl;

import com.evaluationSystem.entity.Questionnaire;
import com.evaluationSystem.entity.SecurityUser;
import com.evaluationSystem.entity.UserDetail;
import com.evaluationSystem.entity.dto.QuestionnaireDto;
import com.evaluationSystem.mapper.*;
import com.evaluationSystem.service.QuestionnaireService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.*;

@Service
@RequiredArgsConstructor
public class QuestionnaireImpl implements QuestionnaireService {

    private final QuestionnaireMapper questionnaireMapper;
    private final AppraiseMapper appraiseMapper;
    private final DimensionMapper dimensionMapper;
    private final ScoreMapper scoreMapper;
    private final WeightMapper weightMapper;

    @Override
    public PageInfo<Questionnaire> getQuestionList(QuestionnaireDto questionnaireDto) {
        PageHelper.startPage(questionnaireDto.getPage(), questionnaireDto.getLimit());
        List<Questionnaire> questionnairesPage = questionnaireMapper.getQuestionList(questionnaireDto);
        for (Questionnaire questionnaire : questionnairesPage) {
            Integer dimensionCount = dimensionMapper.getDimensionCount(questionnaire.getQuestionId());
            Integer SUM = appraiseMapper.getQuestionCountById(questionnaire.getQuestionId());
            Integer COUNT = appraiseMapper.getScoreListByQueId(questionnaire.getQuestionId());
            int sum = SUM == null ? 0 : SUM;
            int count = COUNT == null ? 0 : COUNT;
            questionnaire.setQuestionCount(count + "/" + sum);
            questionnaire.setTitleCount(dimensionCount);
        }
        return new PageInfo<>(questionnairesPage);
    }

    @Override
    public List<Questionnaire> getOwnQuestionList(String mobile) {
        return questionnaireMapper.getOwnQuestionList(mobile);
    }

    @Override
    public Questionnaire getInfo(String id) {
        return questionnaireMapper.getInfo(id);
    }

    @Override
    public HashMap<String, Integer> checkStatus(QuestionnaireDto questionnaireDto) {
        return questionnaireMapper.checkStatus(questionnaireDto);
    }

    @Override
    public void insertQuestion(Questionnaire questionnaireDto) {
        questionnaireDto.setQuestionId("QX" + UUID.randomUUID().toString().replace("-", "").substring(0, 8));
        UserDetail user = SecurityUser.getUser();
        questionnaireDto.setQuestionCreateId(user.getId().toString());
        questionnaireDto.setQuestionCreateName(user.getRealName());
        questionnaireMapper.insertQuestion(questionnaireDto);
    }

    @Override
    public void updateQuestionList(Questionnaire questionnaireDto) {
        questionnaireMapper.updateQuestionList(questionnaireDto);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteQuestion(Collection<? extends Serializable> idList) {
        Map<String, Object> map = new HashMap<>();
        map.put("questionIdList", idList);
        scoreMapper.deleteScoring(map);
        appraiseMapper.deleteAppraise(map);
        dimensionMapper.deleteDimension(map);
//        weightMapper.delWeightList(map);
        weightMapper.delWeight(map);
        questionnaireMapper.deleteQuestion(idList);
    }

}
